package com.koron.standard.excel;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.util.List;

/**
 * @author jzp
 * @since 2021/12/8
 */
@Slf4j
public class ErrorFillWriteHandler implements SheetWriteHandler {

    private CellStyle readCellStyle;
    private Drawing<?> drawingPatriarch;
    private final XSSFClientAnchor xssfClientAnchor = new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 0, 0);
    private final List<ExcelError> excelErrors;

    public ErrorFillWriteHandler(List<ExcelError> excelErrors){
        this.excelErrors = excelErrors;
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        // 构造单元格样式
        if (readCellStyle == null) {
            drawingPatriarch = sheet.createDrawingPatriarch();
            CellStyle cellStyle = writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook().createCellStyle();
            cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
            // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            this.readCellStyle = cellStyle;
        }
        for (ExcelError excelError : excelErrors) {
            int rowIndex = excelError.getRowIndex();
            int collIndex = excelError.getCollIndex();
            Cell cell = sheet.getRow(rowIndex).getCell(collIndex);
            if (cell == null) {
                cell = sheet.getRow(rowIndex).createCell(collIndex);
            }
            drawComment(cell, excelError, true);
        }
    }

    /**
     * 给单元格打上备注信息
     */
    private void drawComment(Cell cell, ExcelError excelError, boolean isStyle) {
        int columnIndex = cell.getColumnIndex();
        int rowIndex = cell.getRowIndex();
        String comment = excelError.getComment();
        Comment cellComment = cell.getCellComment();
        if (cellComment == null) {
            xssfClientAnchor.setCol1(columnIndex);
            xssfClientAnchor.setCol2(columnIndex + 2 + excelError.getCollStep());
            xssfClientAnchor.setRow1(rowIndex);
            xssfClientAnchor.setRow2(rowIndex + 2 + excelError.getRowStep());
            cellComment = drawingPatriarch.createCellComment(xssfClientAnchor);
        } else if (cellComment.getString() != null){
            comment = cellComment.getString().getString() + "；"+ comment;
        }
        cellComment.setString(new XSSFRichTextString(comment));
        cell.setCellComment(cellComment);
        if (isStyle) {
            cell.setCellStyle(this.readCellStyle);
        }
    }
}
